package com.airclick.merchant.repository;

import com.airclick.merchant.entity.MerDomain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * @author caiwangjue
 * @date 8/10/21 4:31 PM
 */
public interface MerRepository extends JpaRepository<MerDomain, String>, JpaSpecificationExecutor {


    @Query(value = "select password from t_ums_admin where user_name = ?1 ",nativeQuery = true)
    String findAllByUsername(String username);

    @Modifying
    @Query(value = "update t_ums_admin set password = ?2 where user_name = ?1 ",nativeQuery = true)
    int updatePassword(String userName, String newpassword);

//    @Query(value = "select * from t_ums_admin where if(?1 !='',status = ?1, 1=1) and if(?2 != '', gmt_create >= ?2 ,1=1) and if(?3 != '', gmt_create <= ?3 ,1=1) ORDER BY gmt_create DESC LIMIT ?4,?5",nativeQuery = true)
//    List<MerDomain> merDomainList(String status,String startTime , String endTime,Integer start,Integer pageSize);

    MerDomain findAllById(String id);

    @Modifying
    @Query(value = "update t_ums_admin set status = ?2 , note = ?3 where id = ?1 " ,nativeQuery = true)
    int updateUser(String id , String status,String note);

    @Query(value = "select count(id) from t_ums_admin WHERE gmt_create >= ?1 and gmt_create <= ?2 and is_super_shop = 0",nativeQuery = true)
    List<String> selectSaveUser(Date startTime, Date getnowEndTime);


    @Query(value = "SELECT date_format( login_time, '%Y-%m-%d' ) AS date , COUNT(id) AS countz FROM t_ums_admin WHERE is_super_shop = 0 and login_time >= date(now()) - INTERVAL ?1 DAY AND status NOT IN(0,5) GROUP BY date",nativeQuery = true)
    List<String> theDailyStatistics(String date);

    @Query(value = "select * from t_ums_admin where user_name = ?1 ",nativeQuery = true)
    MerDomain findAllByUsernameId(String userName);

    @Query(value = "select email from t_ums_admin where user_name = ?1 ",nativeQuery = true)
    String findAllByUsernames(String userName);


//    MerDomain findAllByUsernames(String username);
//    @Modifying
//    @Query(value = "INSERT INTO t_ums_admin (user_name,password,admin_id)VALUES (?1,?2,?3)",nativeQuery = true)
//    int addUser(String userName, String encodePassword, String adminId);
}
